package com.yyysj.system.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yyysj.common.core.constants.Constants;
import com.yyysj.common.core.utils.ThreadLocalUtils;
import com.yyysj.system.domain.po.Post;
import com.yyysj.system.domain.vo.PostVO;
import com.yyysj.system.mapper.PostMapper;
import com.yyysj.system.service.IPostService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
@RequiredArgsConstructor
public class PostServiceImpl implements IPostService {

    private final PostMapper postMapper;

    public PostVO getPostList(String title, Long postId, Long userId, int pageNum, int pageSize) {
        // 1.PageHelper
        PageHelper.startPage(pageNum, pageSize);
        // 2.分页查询
        List<Post> posts = postMapper.selectPostList(title, postId, userId);
        Page<Post> page = (Page<Post>) posts;
        PageInfo<Post> pageInfo = new PageInfo<>(page);
        return new PostVO(posts, pageInfo.getTotal());
    }

    public boolean updatePostStatus(Long postId, Integer status) {

        Long userId = ThreadLocalUtils.get(Constants.USER_ID, Long.class);

        return postMapper.updatePostStatus(postId, status, userId) > 0;
    }

    public boolean deletePost(Long postId) {
        return postMapper.deletePost(postId) > 0;
    }
}